import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ViewController, Events } from 'ionic-angular';
import { KeyBoardProvider } from '../../../providers/keyboard';


@IonicPage()
@Component({
  selector: 'page-keyboard',
  templateUrl: 'keyboard.html',
})
export class KeyboardPage {
  currentValue: string = '';  //当前值
  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public viewCtrl: ViewController,
    public events: Events,
    public keyBoardProvider: KeyBoardProvider,
  ) {
    //接收键盘关闭事件
    this.events.subscribe("hide_key_board", (val) => {
      if (val.currentVal) {
        this.dissmiss();
      }
    })
    //接收传过来的值
    this.events.subscribe("bulr_vaule", (val) => {
      if (val) {
        this.currentValue = val.currentValue;
      }
    })
  }
  //隐藏键盘
  dissmiss() {
    this.viewCtrl.dismiss({
      currentValue: this.currentValue
    });
  }
  //键盘输入事件
  input(val) {
    switch (val) {
      case 'd':
        var s = this.currentValue + '';
        s = s.substring(0, s.length - 1);
        this.currentValue = s;
        break;
      case 'l':
        this.dissmiss();
        break;
      case '.':
        if (this.currentValue.indexOf(val) == -1) {
          this.currentValue = this.currentValue + ".";
        }
        break;
      default:
        if (this.currentValue.indexOf('.') != -1) {
          let index = this.currentValue.indexOf('.');
          let targetLength = this.currentValue.substring(index + 1).length;
          if (targetLength <= 7) {
            this.currentValue = this.currentValue + val;
          }
        } else {
          if (!(this.currentValue == '0' && val == 0)) {
            this.currentValue = this.currentValue + val;
          }
        }
        break;
    }
    this.keyBoardProvider.setKeyboardValue(this.currentValue);
  }
  allDelete() {
    this.currentValue = "";
  }
}
